package com.oasystem.web.controller.member;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oasystem.domain.Member;
import com.oasystem.domain.MemberSummary;
import com.oasystem.domain.PagingBean;
import com.oasystem.domain.SelledGoodsSearchResult;
import com.oasystem.service.DepartmentService;
import com.oasystem.service.GoodsService;
import com.oasystem.service.MemberService;
import com.oasystem.service.OperatorService;

@Controller
@RequestMapping(value = "/MainMenu/members")
public class MembersController {

	@Resource(name = "operatorService")
	private OperatorService operatorService;
	@Resource(name = "goodsService")
	private GoodsService goodsService;
	@Resource(name = "departmentService")
	private DepartmentService departmentService;
	@Resource(name = "memberService")
	private MemberService memberService;

	@RequestMapping(value = "/addMemberPage")
	public String addMemberPage(Model model) {
		model.addAttribute("mainMenu", "members");
		model.addAttribute("subMenu", "add");
		Member member = new Member();
		model.addAttribute("member", member);
		model.addAttribute("operatorList",
				operatorService.getAllOperatorsWithoutPermission());
		model.addAttribute("goodsList", goodsService.getAllGoods());
		model.addAttribute("departmentList",
				departmentService.getAllDepartment());

		return "members.addPage";
	}

	@RequestMapping(value = "/insertMember", method=RequestMethod.POST)
	public @ResponseBody
	ModelMap insertMember(@ModelAttribute("member") Member member) {

		ModelMap model = new ModelMap();
		try {
			memberService.insertMember(member);
			model.addAttribute("isSuccessfull", Boolean.TRUE);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("isSuccessfull", Boolean.FALSE);
		}
		return model;
	}
	
	@RequestMapping(value = "/loadMember", method=RequestMethod.GET)
	public @ResponseBody
	ModelMap loadMember(Long memberID) {
		ModelMap model = new ModelMap();
		model.addAttribute("member",memberService.loadMember(memberID));
		return model;
	}
	
	@RequestMapping(value = "/deleteMember", method=RequestMethod.GET)
	public @ResponseBody
	ModelMap deleteMember(Long memberID) {
		ModelMap model = new ModelMap();
		try{
		memberService.deleteMember(memberID);
		}catch(Exception e){
			model.addAttribute("error","删除失败");
		}
		return model;
	}
	
	@RequestMapping(value = "/updateMember", method=RequestMethod.POST)
	public @ResponseBody
	ModelMap updateMember(@ModelAttribute("member") Member member) {
		ModelMap model = new ModelMap();
		try{
		memberService.updateMember(member);
		}catch(Exception e){
			model.addAttribute("error","修改失败");
		}
		return model;
	}
	
	@RequestMapping(value = "/searchPage")
	public String searchPage(Model model) {
		model.addAttribute("mainMenu", "members");
		model.addAttribute("subMenu", "search");
		model.addAttribute("member", new Member());
		model.addAttribute("operatorList",
				operatorService.getAllOperatorsWithoutPermission());
		model.addAttribute("goodsList", goodsService.getAllGoods());
		model.addAttribute("departmentList",
				departmentService.getAllDepartment());
		return "members.searchPage";
	}

	// search result
	@RequestMapping(value = "/searchResultPage")
	public String searchResultPage(@ModelAttribute("member") Member member,
			Model model) {
		PagingBean<Member> pagingBean = new PagingBean<Member>();
		try {
			pagingBean.setCurrentPage(Integer.valueOf(member.getPageIndex()));
			memberService.searchForPagingList(member, pagingBean);
			model.addAttribute("pagingBean", pagingBean);
			model.addAttribute("member", member);
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return "members.searchResultPage";
	}

	@RequestMapping(value = "/summaryPage")
	public String summaryPage(Model model) {
		model.addAttribute("mainMenu", "members");
		model.addAttribute("subMenu", "summary");
		return "members.summaryPage";
	}

	@RequestMapping(value = "/comprehensiveStatistics")
	public @ResponseBody
	ModelMap comprehensiveStatistics(String from, String to) {
		ModelMap model = new ModelMap();
		try {
			MemberSummary summary = memberService.loadMemberSummary(from, to);
			model.addAttribute("memberSummary", summary);
			model.addAttribute("isSuccessfull", Boolean.TRUE);
		} catch (Exception e) {
			model.addAttribute("isSuccessfull", Boolean.FALSE);
		}
		return model;
	}

	@RequestMapping(value = "/memberGoodsCost")
	public String memberGoodsCost(String from, String to,String pageIndex, Model model) {
		PagingBean<Member> pagingBean = new PagingBean<Member>();
		pagingBean.setCurrentPage(Integer.valueOf(pageIndex));
		memberService.retrieveMemberGoodsCostPagingList(from, to, pagingBean);
		model.addAttribute("pagingBean", pagingBean);
		return "members.memberGoodsCost";
	}
	
	@RequestMapping(value = "/goodsStatistics")
	public String goodsStatistics(String from, String to,String pageIndex, Model model) {
		PagingBean<SelledGoodsSearchResult> pagingBean = new PagingBean<SelledGoodsSearchResult>();
		pagingBean.setCurrentPage(Integer.valueOf(pageIndex));
		memberService.retrieveGoodsStatisticsList(from, to, pagingBean);
		model.addAttribute("pagingBean", pagingBean);
		return "members.goodsStatistics";
	}
}
